import { cloneDeep } from 'lodash';
export interface MetadataField {
  name: string;
  extName?: string;
  changeType?: 'input' | 'select';
  fieldProps?: string[];
  props?: any;
  componentType?: string;
  type?: string;
  defaultOperator?: string;
  operators?: any[];
  valueType?: 'string' | 'date' | 'array' | 'dataArray' | 'image' | 'field' | 'number' | 'multiple';
}
export type MetedataFieldMaps = Record<string, MetadataField>;
let metadataFieldMaps: MetedataFieldMaps = {
  Input: {
    name: tr('单行文本'),
    extName: 'TextFieldPropCmp',
    fieldProps: ['defaultValue', 'maxLength', 'regex'],
    valueType: 'string',
    defaultOperator: 'LIKE',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LIKE', label: '包含' },
      { value: 'NOT_LIKE', label: '不包含' },
      { value: 'START_WITH', label: '以..开头' },
      { value: 'END_WITH', label: '以..结尾' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  TextArea: {
    name: tr('多行文本'),
    extName: 'TextAreaPropCmp',
    fieldProps: ['defaultValue'],
    valueType: 'string',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LIKE', label: '包含' },
      { value: 'NOT_LIKE', label: '不包含' },
      { value: 'START_WITH', label: '以..开头' },
      { value: 'END_WITH', label: '以..结尾' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  InputNumber: {
    name: tr('数字'),
    extName: 'NumberFieldPropCmp',
    fieldProps: ['numberDefaultValue', 'maxValue', 'minValue', 'precision', 'integerOnly', 'isUnitField', 'unit'],
    valueType: 'number',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LT', label: '<' },
      { value: 'LT_EQ', label: '<=' },
      { value: 'GT', label: '>' },
      { value: 'GT_EQ', label: '>=' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  DatePicker: {
    name: tr('日期'),
    changeType: 'select',
    extName: 'DateFieldPropCmp',
    fieldProps: ['dateDefaultValue', 'maxDate', 'minDate', 'dateType'],
    type: 'date',
    valueType: 'date',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LT', label: '<' },
      { value: 'LT_EQ', label: '<=' },
      { value: 'GT', label: '>' },
      { value: 'GT_EQ', label: '>=' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  RangePicker: {
    name: tr('日期区间'),
    changeType: 'select',
    fieldProps: ['rangeDateDefaultValue', 'maxDate', 'minDate', 'dateType'],
    type: 'date',
    valueType: 'dataArray',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LT', label: '<' },
      { value: 'LT_EQ', label: '<=' },
      { value: 'GT', label: '>' },
      { value: 'GT_EQ', label: '>=' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  Selector: {
    name: tr('单选枚举'),
    extName: 'ComboxPropCmp',
    changeType: 'select',
    fieldProps: ['optionsDefalutValue', 'options'],
    props: {
      useStorage: false,
    },
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LIKE', label: '包含' },
      { value: 'NOT_LIKE', label: '不包含' },
      { value: 'START_WITH', label: '以..开头' },
      { value: 'END_WITH', label: '以..结尾' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  MultipleSelect: {
    componentType: 'Selector',
    name: tr('多选枚举'),
    extName: 'MultiComboxPropCmp',
    changeType: 'select',
    fieldProps: ['multipleOptionsDefalutValue', 'options'],
    props: {
      mode: 'multiple',
      useStorage: false,
    },
    type: 'array',
    valueType: 'array',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LIKE', label: '包含' },
      { value: 'NOT_LIKE', label: '不包含' },
      { value: 'START_WITH', label: '以..开头' },
      { value: 'END_WITH', label: '以..结尾' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  // "RadioGorup": {
  // 	name: tr("单选框"),
  // 	extName: "RadioBoxPropCmp",
  // 	fieldProps: ["optionsDefalutValue", "options"]
  // },
  // "CheckboxGorup": {
  // 	name: tr("复选框"),
  // 	extName: "CheckBoxPropCmp",
  // 	fieldProps: ["multipleOptionsDefalutValue", "options"],
  // 	type: "array",
  // 	valueType: "array"
  // },
  InputMoney: {
    name: tr('金额'),
    extName: 'MoneyFieldPropCmp',
    fieldProps: ['defaultValue', 'decimalPrecision', 'unitMoeny'],
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'NOT_EQ', label: '!=' },
      { value: 'LT', label: '<' },
      { value: 'LT_EQ', label: '<=' },
      { value: 'GT', label: '>' },
      { value: 'GT_EQ', label: '>=' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  ImageUpload: {
    name: tr('图片'),
    extName: 'ImagePropCmp',
    changeType: 'select',
    fieldProps: ['maxCount'],
    valueType: 'image',
  },
  FileUpload: {
    name: tr('文件'),
    changeType: 'select',
    extName: 'FilePropCmp',
    fieldProps: ['maxCount'],
    valueType: 'field',
  },
  UserSelector: {
    name: tr('用户'),
    extName: 'UserSelectorPropCmp',
    changeType: 'select',
    fieldProps: ['multiple'],
    valueType: 'multiple',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  CodeList: {
    name: tr('编码'),
    changeType: 'select',
    extName: 'CodeListPropCmp',
    type: 'codelist',
    fieldProps: ['codeType'],
    valueType: 'string',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  GroupSelector: {
    name: tr('组织'),
    changeType: 'select',
    extName: 'OrgSelectorPropCmp',
    fieldProps: ['multiple'],
    valueType: 'multiple',
    defaultOperator: 'EQ',
    operators: [
      { value: 'EQ', label: '=' },
      { value: 'IN', label: '属于' },
      { value: 'NOT_IN', label: '不属于' },
      { value: 'IS_NULL', label: '为空' },
      { value: 'IS_NOT_NULL', label: '不为空值' },
    ],
  },
  RelateEntry: {
    name: tr('关联对象'),
    changeType: 'select',
    extName: 'RelateEntryPropCmp',
    defaultOperator: 'EQ',
    operators: [{ value: 'EQ', label: '=' }],
  },
};

export function getFieldMaps() {
  return cloneDeep(metadataFieldMaps);
}

export function setFieldMaps(maps: MetedataFieldMaps) {
  metadataFieldMaps = { ...metadataFieldMaps, ...maps };
  return metadataFieldMaps;
}

export function fieldDataSource() {
  const propComponentDataSource: any[] = [];
  const fieldMaps = getFieldMaps();
  Object.keys(fieldMaps).map((typeName: string) => {
    const label = fieldMaps[typeName].name;
    const value = fieldMaps[typeName].extName ? fieldMaps[typeName].extName : typeName;
    propComponentDataSource.push({ label, value });
  });
  return propComponentDataSource;
}
